package cn.zwx.common.swagger.starter.config;

import cn.zwx.common.swagger.starter.properties.SwaggerProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.Contact;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import javax.annotation.Resource;

/**
 * @author zhangwenxue
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperty.class)
public class SwaggerConfiguration {

    private final String PROD = "prod";

    @Resource
    private SwaggerProperty swaggerProperty;

    @Bean
    public Docket createRestApi() {
        Docket build;
        if (!this.getProFile()){
            build = StringUtils.isBlank(swaggerProperty.getBasePackage()) ?
                    // uat/test环境需要配置信息
                    new Docket(DocumentationType.SWAGGER_2)
                            .enable(Boolean.TRUE)
                            .apiInfo(apiInfo())
                            .select()
                            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                            //不显示错误的接口地址
                            .paths(PathSelectors.regex("^(?!/error.*).*$")) // 排除 /error.* 路径
                            .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
                            .paths(PathSelectors.any())
                            .paths(PathSelectors.any()).build()
                    :new Docket(DocumentationType.SWAGGER_2)
                    .enable(Boolean.TRUE)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(swaggerProperty.getBasePackage()))
                    //不显示错误的接口地址
                    .paths(PathSelectors.regex("^(?!/error.*).*$")) // 排除 /error.* 路径
                    .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
                    .paths(PathSelectors.any())
                    .build();
        }else {
            // 生产环境不需要配置任何信息
            build = new Docket(DocumentationType.SWAGGER_2).enable(Boolean.FALSE).apiInfo(apiInfoDefault()).select().build();
        }
        return build;
    }

    private Boolean getProFile() {
        return swaggerProperty.getProfiles().equals(PROD)?Boolean.TRUE:Boolean.FALSE;
    }

    @Bean
    public OpenAPI api() {
        if (this.getProFile()){
            return new OpenAPI().info(new Info().title("yun-shop接口文档")
                    .description("默认接口文档")
                    .version("v1.0.0"));
        }
        Contact contact = new Contact();
        contact.email(swaggerProperty.getEmail());
        contact.name(swaggerProperty.getAuthor());
        contact.url(swaggerProperty.getUrl());
        return new OpenAPI().info(new Info().title(swaggerProperty.getSwaggerTitle())
                .description(swaggerProperty.getDescription())
                .version(swaggerProperty.getVersion()).contact(contact));
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerProperty.getSwaggerTitle())
                .description(swaggerProperty.getDescription())
                .termsOfServiceUrl(swaggerProperty.getTermsOfServiceUrl())
                .contact(new springfox.documentation.service.Contact(swaggerProperty.getAuthor(),swaggerProperty.getUrl(),swaggerProperty.getEmail()))
                .version(swaggerProperty.getVersion())
                .build();
    }
    private ApiInfo apiInfoDefault() {
        return new ApiInfoBuilder()
                .title("默认版本")
                .description("默认标题")
                .termsOfServiceUrl("http://localhost:8080")
                .contact(new springfox.documentation.service.Contact("default","",""))
                .version("1.0.0")
                .build();
    }

}
